package com.summer.main;

import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CharsetTest {
	
    public static String formatTime(long time){
    	long unit       = 1;//1000;
        long nextUnit   = unit * 60;
        long second     = (time % nextUnit) / unit;

        unit        = nextUnit;
        nextUnit    = unit * 60;
        long minutes    = (time % nextUnit) / unit;

        unit        = nextUnit;
        nextUnit    = unit * 24;
        long hours  = (time % nextUnit) / unit;

        unit        = nextUnit;
        nextUnit    = unit * 365;
        long days   = (time % nextUnit) / unit;

//        unit = unit * 12;
//        long months     = time % unit / 1000;

        long years      = (time - days) / nextUnit;

        StringBuilder builder = new StringBuilder();

        int countShow = 0;
//        int limit = showLimit < 0 ? Integer.MAX_VALUE : showLimit;
        int limit = 100;

        if(years > 0){
            countShow++;
            builder.append(String.format("%d年零", years));
        }
//        if(months > 0)
//            builder.append(String.format("%02d月", months));
        if(days > 0) {
            countShow++;
            builder.append(String.format("%d天", days));
        }
        if(hours > 0 & (countShow<limit)) {
            countShow++;
            builder.append(String.format("%d小时", hours));
        }
        if(minutes > 0 & (countShow<limit)) {
            countShow++;
            builder.append(String.format("%d分", minutes));
        }
        if(second > 0 & (countShow<limit))
            builder.append(String.format("%d秒", second));

        return builder.toString();
    }
	
	public static void main(String[] args) {  
		System.out.println(formatTime(365 * 24 * 60 * 60 + 58 * 24 * 60 * 60 + 21 * 60 * 60 + 50 * 60 + 37));
		System.out.println(formatTime(58 * 24 * 60 * 60 + 21 * 60 * 60 + 50 * 60 + 37));
		System.out.println(formatTime(21 * 60 * 60 + 50 * 60 + 37));
		System.out.println(formatTime(30 * 60 + 37));
//
//		String str = "1";
//		long time = System.currentTimeMillis();
////		for(int index=0;index < 10000;index++){
////			str += "1";
////			System.out.println(isNumeric(str)); 
////		}
////		str +=".";
//		for(int index=1;index < 20;index++){
//			str += index;
//			String tmp = str+"."+index;
//			System.out.print(tmp + "  =  "); 
//			System.out.print(valueToFloat(tmp,0) + "  =  "); 
//			System.out.println(valueToInt(tmp,0)); 
//		}
//		System.out.println("use time " + (System.currentTimeMillis()-time));  
//		
//		
//		
//		System.out.println(valueToInt("3213",0));  
//		System.out.println(valueToInt("121.12",0));  
//		System.out.println(valueToInt("0.1",0));  
//		System.out.println(valueToInt("1.0",0));  
//		System.out.println(valueToInt("1a",0));  
//		
//		
//		System.out.println(isNumeric("3213"));  
//		System.out.println(isNumeric("121.12"));  
//		System.out.println(isNumeric("1a"));  
//		
//        System.out.println("Default Charset=" + Charset.defaultCharset());  
//        System.out.println("file.encoding=" + System.getProperty("file.encoding"));  
//        System.out.println("Default Charset=" + Charset.defaultCharset());  
//        System.out.println("Default Charset in Use=" + getDefaultCharSet());  
//        
//        String url = "https://p.qiremanhua.com/uploads/chapter/10563/201812/5c0f737ce6557.JPG?x-oss-process=image/quality,q_90/format,webp";
//        Pattern pattern = Pattern.compile("chapter/\\d+/\\d+/([a-z0-9]+)\\.(jpg|jpeg|gif|png)",Pattern.CASE_INSENSITIVE);
//        Matcher matcher = pattern.matcher(url);
//        if(matcher.find()){
//        	System.out.println(matcher.group(1));
//        }
    }  
  
    private static String getDefaultCharSet() {  
        OutputStreamWriter writer = new OutputStreamWriter(new ByteArrayOutputStream());  
        String enc = writer.getEncoding();  
        return enc;  
    }  
    
    
    
    
    public static boolean isEmpty(String str){
        return str == null || str.length() == 0;
    }
    public static boolean isNumeric(String str){
        int dotCount = 0;
        for(int i=str.length();--i>=0;){
            char singleChar =  str.charAt(i);
            if('.' == singleChar){
                dotCount++;
                if(dotCount<=1)
                    continue;
                else
                    return false;
            }
            if (!Character.isDigit(singleChar)){
                return false;
            }
        }
        return true;
    }

    public static Float valueToFloat(String value,float def) {
        if(isEmpty(value))
            return def;
        if(!isNumeric(value))
            return def;
        
        return new BigDecimal(value).floatValue();
//        return Float.valueOf(value);
    }
    public static int valueToInt(String value,int def) {
    	if(isEmpty(value))
            return def;
        if(!isNumeric(value))
            return def;
        
        return new BigDecimal(value).intValue();
//        return Integer.valueOf(value);
    }
    
}
